package com.esteel.coroutine.net

import com.esteel.coroutine.model.ArticalBean
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * Created by Android Studio.
 * User: 赵宏图
 * Date: 2019/8/21
 * Time: 10:50
 * Email:zhaohongtu@payworth.cn
 * Desc:
 */
object RetrofitManager {

    private var retrofit: Retrofit? = null

    private fun getInstance(): Retrofit {
        if (retrofit == null) {
            val okHttpClient = OkHttpClient.Builder()
                    .connectTimeout(5000, TimeUnit.MILLISECONDS)
                    .readTimeout(5000, TimeUnit.MILLISECONDS)
                    .retryOnConnectionFailure(true).build()
            retrofit = Retrofit.Builder().baseUrl("https://wanandroid.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient).build()

        }
        return retrofit!!
    }


    fun getArtical(): ArticalBean? {
        val artical = getInstance().create(ApiService::class.java)
                .getArtical().execute()
        if (artical.isSuccessful) {
            return artical.body()
        } else if (artical.code() == 404) {
            throw Exception("404 Not Found")
        }
        return null
    }
}